/**
 * 所属节点：库存查询
 * 创建人：zm
 * 创建时间：2024-08-19
 * 最后修改人：zm
 */
cb.define(process.env.__DOMAINKEY__, ["common/common_VM.Extend.js"], function (common) {
  let GDGY01_isv_storequery_VM_Extend = {
    doAction: function (name, viewModel) {
      if (this[name]) this[name](viewModel);
    },
    init: function (viewModel) {
      viewModel.on("customInit", function (data) {
        let tenantID = viewModel.getAppContext().tenant.tenantId;
        let userInfo = viewModel.getAppContext().user;
        const qianzhui = "/gdgy/pc/isv2";
        viewModel.getParams().autoLoad = false;
        viewModel.pageNow = 1;
        viewModel.pageSizeNow = 10;
        var myContext = viewModel.getAppContext();
        var gridModel = viewModel.getGridModel();
        viewModel.on("afterMount", function () {
          const filtervm = viewModel.getCache("FilterViewModel");
          filtervm.on("afterInit", function () {
            viewModel.getCache("FilterViewModel").getParams().filterRows = 2;
            viewModel.getParams().autoLoad = false;
            var gridModelInfo = viewModel.getGridModel();
            gridModelInfo.clear();
            var fv = viewModel.getCache("FilterViewModel").getData();
            //console.log(fv);
            var locationCode = fv.locationCode.value1;
            var locationName = fv.locationCode.value1;
            var product_name = fv.product_name.value1;
            // var rfid_code = fv.rfid_code.value1;
            var warehouse_name = fv.warehouse_name.value1;
            var product_sku_name = fv.product_sku_name.value1;
            //debugger
            var reqParams = {
              locationCode: locationCode,
              locationName: locationName,
              product_name: product_name,
              warehouse_name: warehouse_name,
              product_sku_name: product_sku_name,
              pageNow: viewModel.pageNow,
              pageSize: viewModel.pageSizeNow,
            };
            //console.log(reqParams);
            setLocationData(viewModel, reqParams);

            //检索触发
            viewModel.on("beforeSearch", function (params) {
              // console.log("[beforeSearch]");
              var locationName = filtervm.get("locationName").getFromModel().getValue();
              var locationCode = filtervm.get("locationCode").getFromModel().getValue();
              var product_name = filtervm.get("product_name").getFromModel().getValue();
              var rfid_code = filtervm.get("rfid_code").getFromModel().getValue();
              var warehouse_name = filtervm.get("warehouse_name").getFromModel().getValue();
              var product_sku_name = filtervm.get("product_sku_name").getFromModel().getValue();
              var reqParams = {
                locationCode: locationCode,
                locationName: locationName,
                rfid_code: rfid_code,
                product_name: product_name,
                warehouse_name: warehouse_name,
                product_sku_name: product_sku_name,
                pageNow: viewModel.pageNow,
                pageSize: viewModel.pageSizeNow,
              };
              setLocationData(viewModel, reqParams);
              return false;
            });
          });
        });

        function setLocationData(viewModel, reqParams) {
          let gridModelInfo = viewModel.getGridModel();
          let params = viewModel.getParams();
          reqParams.domainKey = params.domainKey;
          reqParams.params = params;
          //reqParams.currentUser = Object.assign(myContext.tenant,myContext.user);
          //console.log("[==]"+JSON.stringify(params));
          //console.log("[==]"+JSON.stringify(reqParams));
          //cb2a1e81ba89446fa428d1c5d3b32091 Idx3.storeQuery.storeQuery

          /**
           * 张明迁移
           */
          const url = qianzhui + "/epclist/EpcPageForReact?tenant_id=" + tenantID;
          const proxy = viewModel.setProxy({
            storeQuery: {
              url: url,
              method: "POST",
            },
          });
          proxy.storeQuery({ ...reqParams }, function (err, res) {
            if (err) {
              cb.utils.alert(err.message);
            } else {
              if (res.status == 1) {
                let datalist = res.dataList;
                gridModelInfo.setState("dataSourceMode", "local");
                gridModelInfo.setDataSource(datalist);
                gridModelInfo.setReadOnly(true);
                gridModelInfo.setPageInfo({
                  pageSize: viewModel.pageSizeNow,
                  pageIndex: viewModel.pageNow,
                  recordCount: res.totalSize,
                });
              } else {
                let msg = typeof res.message == "undefined" ? "接口异常" : res.message;
                cb.utils.alert(msg);
              }
            }
          });

          // let rest = cb.rest.invokeFunction(
          //   "GDGY01.storeQuery.storeQueryAPI",
          //   reqParams,
          //   function (err, res) {},
          //   viewModel,
          //   { async: false }
          // );

          // console.log("[setLocationData]", JSON.stringify(rest));
          // let res = rest.result;
          // if (res.status == 1) {
          //   let datalist = res.dataList;
          //   gridModelInfo.setState("dataSourceMode", "local");
          //   gridModelInfo.setDataSource(datalist);
          //   gridModelInfo.setReadOnly(true);
          //   gridModelInfo.setPageInfo({
          //     pageSize: viewModel.pageSizeNow,
          //     pageIndex: viewModel.pageNow,
          //     recordCount: res.totalSize,
          //   });
          // } else {
          //   let msg = typeof res.message == "undefined" ? "接口异常" : res.message;
          //   cb.utils.alert(msg);
          // }
        }

        gridModel.on("pageInfoChange", function () {
          const filtervm = viewModel.getCache("FilterViewModel");
          //获取当前页码
          var pageIndex = gridModel.getPageIndex();
          //获取当前页条数
          var pageSize = gridModel.getPageSize();
          viewModel.pageNow = pageIndex;
          viewModel.pageSizeNow = pageSize;
          var locationName = filtervm.get("locationName").getFromModel().getValue();
          var locationCode = filtervm.get("locationCode").getFromModel().getValue();
          var product_name = filtervm.get("product_name").getFromModel().getValue();
          var rfid_code = filtervm.get("rfid_code").getFromModel().getValue();
          var warehouse_name = filtervm.get("warehouse_name").getFromModel().getValue();
          var product_sku_name = filtervm.get("product_sku_name").getFromModel().getValue();
          var reqParams = {
            locationCode: locationCode,
            locationName: locationName,
            rfid_code: rfid_code,
            product_name: product_name,
            warehouse_name: warehouse_name,
            product_sku_name: product_sku_name,
            pageNow: viewModel.pageNow,
            pageSize: viewModel.pageSizeNow,
          };
          setLocationData(viewModel, reqParams);
        });
      });

      //button0fj
      viewModel.get("button0fj") &&
        viewModel.get("button0fj").on("click", function (row) {
          // 详细--单击
          //console.log("[c-button0fj]"+JSON.stringify(row))
          //{"index":1,"cItemName":"button0fj","name":"isv_storequery_1570889597096820740"}
          var rowdata = viewModel.getGridModel().getRow(row.index);
          // console.log(JSON.stringify(rowdata))
          /*{"rfid_code":"B0101172","locationName":"开发测试货架03-开发测试货位03",
    "warehouse_name":"第一工厂附属仓库","rfidcode_byproduct":"A010689400004018A0E4486D",
    "product_id":"1576870467902373889","product_sku_name":"荣耀手机",
    "id":"1596121800423505928","locationCode":"kfschw03",
    "product_name":"荣耀手机","f_stock_count":2,"_id":"rowId_3"}
    */
          var params = viewModel.getParams();
          var myContext = viewModel.getAppContext();
          var yht_access_token = myContext.yat;
          var options = {
            domainKey: params.domainKey,
            yht_access_token: yht_access_token,
          };
          let data = {
            domainKey: params.domainKey,
            billtype: "VoucherList", // 单据类型
            billno: "isv_storequery", // 单据号
            params: {
              mode: "browse", // (编辑态、新增态、浏览态)
              butReturn: true, //显示底部按钮
              reqParams: {
                locationCode: rowdata.locationCode,
                locationEpc: rowdata.rfid_code,
                product_name: rowdata.product_name,
                product_sku_name: rowdata.product_sku_name,
                product_id: rowdata.product_id,
                locationID: rowdata.id,
                warehouseID: rowdata.warehouse_id,
                warehouse_name: rowdata.warehouse_name,
              },
            },
          };
          cb.loader.runCommandLine("bill", data, viewModel);
        });
    },
  };
  try {
    module.exports = GDGY01_isv_storequery_VM_Extend;
  } catch (error) {}
  return GDGY01_isv_storequery_VM_Extend;
});
